package com.weedsdream.app.function

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.unit.IntOffset
import com.weedsdream.app.colorplate.MAX_PEN_SIZE
import com.weedsdream.app.compose.boxOffset
import com.weedsdream.app.compose.boxScale
import com.weedsdream.app.compose.scCenter
import com.weedsdream.app.struct.drafts


/**全览画板*/
fun fullView() {
    if (scCenter == null) return
    var x0 = Float.MAX_VALUE
    var y0 = Float.MAX_VALUE
    var x1 = Float.MIN_VALUE
    var y1 = Float.MIN_VALUE
    for (paths in drafts.values) for (path in paths) for (p in path.points) {
        if (x0 > p.x) x0 = p.x
        if (x1 < p.x) x1 = p.x
        if (y0 > p.y) y0 = p.y
        if (y1 < p.y) y1 = p.y
    }
    if (x0 == Float.MAX_VALUE) {//当前没有点
        boxScale = 1f
        boxOffset = IntOffset.Zero
    } else {
        boxScale = MAX_PEN_SIZE.let {//边界预留
            minOf(scCenter!!.x * 2 / (x1 - x0 + it), scCenter!!.y * 2 / (y1 - y0 + it), 1f)
        }
        val oft = (scCenter!! - Offset((x0 + x1) / 2, (y0 + y1) / 2)) * boxScale
        boxOffset = IntOffset(oft.x.toInt(), oft.y.toInt())
    }
}